﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace EcSalseManage.EcSalse
{
	public partial class Report_Commission : System.Web.UI.Page
	{

		protected void Page_Load(object sender, EventArgs e)
		{
            if (!IsPostBack)
            {

                ViewState["ProjectID"] = Request.QueryString["id"].ToString();

                this.Report_Tab1.ExchangeTypeID = "11";  //标识为项目页Salse_Detail.aspx
                this.Report_Tab1.MainID = ViewState["ProjectID"].ToString();

                if (Request.QueryString["AgencyType"] == null) //从Project_Detail.aspx传来的
                {
                    this.countTable.Visible = false;
                }
                else
                {
                  
                    string AgencyType = Request.QueryString["AgencyType"].ToString();
                    this.gvwConfirmation.Visible = true;
                    DataBind();
                    switch (AgencyType)
                    {
                        case "1":
                            this.lbtnEarlyPlot.CssClass = "asel";
                            break;
                        case "2":
                            this.lbtnSalseAgency.CssClass = "asel";
                            break;
                        case "3":
                            this.lbtnLease.CssClass = "asel";
                            break;
                        case "4":
                            this.lbtnUnionComm.CssClass = "asel";
                            break;
                    }

                    //统计收佣情况
                    Count(AgencyType);

                }


       

   

                DataAccess.Operate.DA_Project_AgencyType_Inherit Project_AgencyType_Inherit = new DataAccess.Operate.DA_Project_AgencyType_Inherit();
                DataSet ds = Project_AgencyType_Inherit.FindProjectAllByID(ViewState["ProjectID"].ToString());

                
                for(int i=0;i<ds.Tables[0].Rows.Count;i++)
                {
                    switch (ds.Tables[0].Rows[i]["Project_AgencyType_TypeID"].ToString())
                    { 
                        case "1":
                            this.lbtnEarlyPlot.Visible = true;
                            break;
                        case "2":
                            this.lbtnSalseAgency.Visible=true;
                            break;
                        case "3":
                            this.lbtnLease.Visible = true;
                            break;
                        case"4":
                            this.lbtnUnionComm.Visible = true;
                            break;
                    }
                }
            }
		}


        /// <summary>
        /// 统计函数
        /// </summary>
        /// <param name="AgencyType"></param>
        private void Count(string AgencyType)
        {
            object obj1, obj2, obj3, obj4;
            DataAccess.Operate.DA_Project_ProjectCommission_Inherit Project_ProjectCommission_Inherit = new DataAccess.Operate.DA_Project_ProjectCommission_Inherit();
            Project_ProjectCommission_Inherit.GetValue(ViewState["ProjectID"].ToString(), AgencyType, out obj1, out obj2, out obj3, out obj4);

            this.Label1.Text = obj1.ToString(); //合计成交套数
            this.Label2.Text = obj2.ToString(); //合计成交金额
            this.Label3.Text = obj3.ToString(); //预计可收到佣金
            this.Label4.Text = obj4.ToString(); //已收到佣金
            this.Label5.Text = ((decimal)obj3 - (decimal)obj4).ToString(); //待收回佣金
        }

        public void DataBind()
        {
            DataAccess.Operate.DA_Project_Confirmation_Inherit Project_Confirmation_Inherit = new DataAccess.Operate.DA_Project_Confirmation_Inherit();
            DataSet ds = Project_Confirmation_Inherit.FindByProjectIDAndAgentID(ViewState["ProjectID"].ToString(), Request.QueryString["AgencyType"].ToString());
            this.gvwConfirmation.DataSource = ds.Tables[0];
            this.gvwConfirmation.DataBind();
        }

        /// <summary>
        ///  让GridView在无数据时显示
        /// </summary>
        /// <param name="EmptyGridView"></param>
        /// <param name="FieldNames"></param>
        public static void renderEmptyGridView(GridView EmptyGridView, string FieldNames)
        {
            //將GridView變成只有Header和Footer列，以及被隱藏的空白資料列
            DataTable dTable = new DataTable();
            char[] delimiterChars = { ',' };
            string[] colName = FieldNames.Split(delimiterChars);
            foreach (string myCol in colName)
            {
                DataColumn dColumn = new DataColumn(myCol.Trim());
                dTable.Columns.Add(dColumn);
            }
            DataRow dRow = dTable.NewRow();

            foreach (string myCol in colName)
            {
                dRow[myCol.Trim()] = DBNull.Value;
            }
            dTable.Rows.Add(dRow);

            EmptyGridView.DataSourceID = null;
            EmptyGridView.DataSource = dTable;
            EmptyGridView.DataBind();
            EmptyGridView.Rows[0].Visible = false;


        }

        protected void lbtnEarlyPlot_Click(object sender, EventArgs e)
        {
            this.Response.Redirect("Project_Commission.aspx?id=" + ViewState["ProjectID"].ToString() + "&AgencyType=1&actionType=2");
        }

        protected void lbtnSalseAgency_Click(object sender, EventArgs e)
        {
            this.Response.Redirect("Project_Commission.aspx?id=" + ViewState["ProjectID"].ToString() + "&AgencyType=2&actionType=2");
        }

        protected void lbtnLease_Click(object sender, EventArgs e)
        {
            this.Response.Redirect("Project_Commission.aspx?id=" + ViewState["ProjectID"].ToString() + "&AgencyType=3&actionType=2");
        }

        protected void lbtnUnionComm_Click(object sender, EventArgs e)
        {
            this.Response.Redirect("Project_Commission.aspx?id=" + ViewState["ProjectID"].ToString() + "&AgencyType=4&actionType=2");
        }

        protected void gvwConfirmation_PreRender(object sender, EventArgs e)
        {
            if (gvwConfirmation.Rows.Count == 0)
            {
                renderEmptyGridView(gvwConfirmation, "Project_Confirmation_ID,Project_Confirmation_NO,Project_Confirmation_Amount,Project_Confirmation_Date,Project_Confirmation_DeveloperAmount,Project_Confirmation_PartCommission,Project_Confirmation_CommissionDate,Project_Confirmation_Status");
            }
        }

        protected void gvwConfirmation_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Insert")
            {
                DataAccess.Operate.DA_Project_ProjectCommission_Inherit Project_ProjectCommission_Inherit = new DataAccess.Operate.DA_Project_ProjectCommission_Inherit();
                DataEntity.T_Project_ProjectCommission Project_ProjectCommission = Project_ProjectCommission_Inherit.FindByProjectIDAndAgentID(ViewState["ProjectID"].ToString(), Request.QueryString["AgencyType"].ToString());

                DataAccess.Operate.DA_Project_Confirmation_Operate Project_Confirmation_Operate = new DataAccess.Operate.DA_Project_Confirmation_Operate();
                DataEntity.T_Project_Confirmation Project_Confirmation = new DataEntity.T_Project_Confirmation();
                TextBox ConfirmationNO = (TextBox)gvwConfirmation.FooterRow.FindControl("txtNo");
                TextBox ConfirmationAmount = (TextBox)gvwConfirmation.FooterRow.FindControl("txtAmount");
                TextBox ConfirmationDate = (TextBox)gvwConfirmation.FooterRow.FindControl("txtDate");

                Project_Confirmation.Project_Confirmation_Amount =Convert.ToDecimal(ConfirmationAmount.Text.Trim());
                Project_Confirmation.Project_Confirmation_NO = ConfirmationNO.Text.Trim();
                Project_Confirmation.Project_Confirmation_Date = Convert.ToDateTime(ConfirmationDate.Text.Trim());
                Project_Confirmation.Project_Confirmation_CommissionID = Project_ProjectCommission.Project_ProjectCommission_ID;

                Project_Confirmation_Operate.Insert(Project_Confirmation);
                DataBind();

            }
        }

        protected void gvwConfirmation_RowEditing(object sender, GridViewEditEventArgs e)
        {
            this.gvwConfirmation.EditIndex = e.NewEditIndex;
            DataBind();
            string id = this.gvwConfirmation.DataKeys[e.NewEditIndex].Value.ToString();
            DataAccess.Operate.DA_Project_Confirmation_Operate Project_Confirmation_Operate = new DataAccess.Operate.DA_Project_Confirmation_Operate();
            DataEntity.T_Project_Confirmation Project_Confirmation =(DataEntity.T_Project_Confirmation) Project_Confirmation_Operate.SelectOne(id);
            if (Project_Confirmation.Project_Confirmation_Status.IsNull)
            {
                TextBox tmpControl1 = (TextBox)gvwConfirmation.Rows[e.NewEditIndex].FindControl("txtCommissionDate");
                TextBox tmpControl2 = (TextBox)gvwConfirmation.Rows[e.NewEditIndex].FindControl("txtPartCommission");
                TextBox tmpControl3 = (TextBox)gvwConfirmation.Rows[e.NewEditIndex].FindControl("txtEditNO");
                TextBox tmpControl4 = (TextBox)gvwConfirmation.Rows[e.NewEditIndex].FindControl("txtEditAmount");
                TextBox tmpControl5 = (TextBox)gvwConfirmation.Rows[e.NewEditIndex].FindControl("txtEditDate");
                tmpControl1.Visible = false;
                tmpControl2.Visible = false;
                tmpControl3.Enabled = false;
                tmpControl4.Enabled = false;
                tmpControl5.Enabled = false;
            }
            else
            {
                TextBox tmpControl1 = (TextBox)gvwConfirmation.Rows[e.NewEditIndex].FindControl("txtEditAmount");
                TextBox tmpControl2 = (TextBox)gvwConfirmation.Rows[e.NewEditIndex].FindControl("txtEditNO");
                TextBox tmpControl3 = (TextBox)gvwConfirmation.Rows[e.NewEditIndex].FindControl("txtEditDate");
                DropDownList tmpControl4 = (DropDownList)gvwConfirmation.Rows[e.NewEditIndex].FindControl("ddlStatus");
                tmpControl4.SelectedValue = "1";
                TextBox tmpControl5 = (TextBox)gvwConfirmation.Rows[e.NewEditIndex].FindControl("txtDeveloperAmount");
                tmpControl1.Enabled = false;
                tmpControl2.Enabled = false;
                tmpControl3.Enabled = false;
                tmpControl4.Enabled = false;
                tmpControl5.Enabled = false;
            }
        }

        protected void gvwConfirmation_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            if(((this.gvwConfirmation.Rows[e.RowIndex].FindControl("ddlStatus") as DropDownList).SelectedValue == "0"))
            {
                this.gvwConfirmation.EditIndex = -1;
                DataBind();
                return ; // 如果是未确认状态 ，不做任何操作
            }
            if ((this.gvwConfirmation.Rows[e.RowIndex].FindControl("ddlStatus") as DropDownList).SelectedValue == "1" && (this.gvwConfirmation.Rows[e.RowIndex].FindControl("txtDeveloperAmount") as TextBox).Text == "")
            {
                Response.Write("<script>alert('请填写发展商确认佣金');</script>");
                return;
            }

            DataAccess.Operate.DA_Project_ProjectCommission_Inherit Project_ProjectCommission_Inherit = new DataAccess.Operate.DA_Project_ProjectCommission_Inherit();
            DataEntity.T_Project_ProjectCommission Project_ProjectCommission = Project_ProjectCommission_Inherit.FindByProjectIDAndAgentID(ViewState["ProjectID"].ToString(), Request.QueryString["AgencyType"].ToString());

            DataEntity.T_Project_Confirmation Project_Confirmation = new DataEntity.T_Project_Confirmation();
            Project_Confirmation.Project_Confirmation_ID =new Guid(this.gvwConfirmation.DataKeys[e.RowIndex].Value.ToString());
            Project_Confirmation.Project_Confirmation_CommissionID = Project_ProjectCommission.Project_ProjectCommission_ID;
            Project_Confirmation.Project_Confirmation_NO = (this.gvwConfirmation.Rows[e.RowIndex].FindControl("txtEditNO") as TextBox).Text;
            Project_Confirmation.Project_Confirmation_Date =Convert.ToDateTime((this.gvwConfirmation.Rows[e.RowIndex].FindControl("txtEditDate") as TextBox).Text);
            Project_Confirmation.Project_Confirmation_Amount =Convert.ToDecimal((this.gvwConfirmation.Rows[e.RowIndex].FindControl("txtEditAmount") as TextBox).Text);
            Project_Confirmation.Project_Confirmation_Status =Convert.ToInt32((this.gvwConfirmation.Rows[e.RowIndex].FindControl("ddlStatus") as DropDownList).SelectedValue);
            Project_Confirmation.Project_Confirmation_DeveloperAmount =Convert.ToDecimal((this.gvwConfirmation.Rows[e.RowIndex].FindControl("txtDeveloperAmount") as TextBox).Text);
            if ((this.gvwConfirmation.Rows[e.RowIndex].FindControl("txtCommissionDate") as TextBox).Text != "")
                Project_Confirmation.Project_Confirmation_CommissionDate = Convert.ToDateTime((this.gvwConfirmation.Rows[e.RowIndex].FindControl("txtCommissionDate") as TextBox).Text);
            if ((this.gvwConfirmation.Rows[e.RowIndex].FindControl("txtPartCommission") as TextBox).Text != "")
            {
                Project_Confirmation.Project_Confirmation_PartCommission = Convert.ToDecimal((this.gvwConfirmation.Rows[e.RowIndex].FindControl("txtPartCommission") as TextBox).Text);
                Project_Confirmation.Project_Confirmation_Status = 2;

            }

            DataAccess.Operate.DA_Project_Confirmation_Operate Project_Confirmation_Operate = new DataAccess.Operate.DA_Project_Confirmation_Operate();
            Project_Confirmation_Operate.Update(Project_Confirmation);
            this.gvwConfirmation.EditIndex = -1;
            DataBind();


            //统计收佣情况
            Count(Request.QueryString["AgencyType"].ToString());

            //如果是已收佣状态，则提醒
            if( Project_Confirmation.Project_Confirmation_Status == 2)
            {
                //提醒项经/营销[副]总监/部门负责人已收佣
                SendMessage(ViewState["ProjectID"].ToString());
            }
        }

        public void SendMessage(string projectID)
        {
            DataAccess.Operate.DA_Project_Inherit Project_Inherit = new DataAccess.Operate.DA_Project_Inherit();
            DataTable dt = Project_Inherit.getCodeAndName(projectID);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string sDirecter = dt.Rows[i]["Project_Directer"].ToString();
                string sDirecterCode = dt.Rows[i]["Project_DirecterCode"].ToString();
                EcSalseManage.App_Code.CommonFunction.SendMessage(sDirecter, sDirecterCode, "测试123", "123");

                string sDirecter1Code = dt.Rows[i]["Project_Directer1Code"].ToString();
                string sDirecter1 = dt.Rows[i]["Project_Directer1"].ToString();
                EcSalseManage.App_Code.CommonFunction.SendMessage(sDirecter1, sDirecter1Code, "测试123", "123");


                string sManagerCode = dt.Rows[i]["Project_ManagerCode"].ToString();
                string sManager = dt.Rows[i]["Project_Manager"].ToString();
                EcSalseManage.App_Code.CommonFunction.SendMessage(sManager, sManagerCode, "测试123", "123");

                string sBusiness = dt.Rows[i]["Project_Business"].ToString();
                string sBusinessCode = dt.Rows[i]["Project_BusinessCode"].ToString();
                EcSalseManage.App_Code.CommonFunction.SendMessage(sBusiness, sBusinessCode, "测试123", "123");


                string sDepartmentHead = dt.Rows[i]["Project_DepartmentHead"].ToString();
                string sDepartmentHeadCode = dt.Rows[i]["Project_DepartmentHeadCode"].ToString();
                EcSalseManage.App_Code.CommonFunction.SendMessage(sDepartmentHead, sDepartmentHeadCode, "测试123", "123");
            }
        }


        protected void gvwConfirmation_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvwConfirmation.EditIndex = -1;
            this.DataBind();
        }


	}
}